#if BACKEND
using MultiAgentsServer.Scripts.Database;
#endif
using System.Text.Json.Serialization;
using MultiAgentsServer.Scripts.Shared;
using MultiAgentsShared;
using OpenAI.ObjectModels;
using OpenAI.ObjectModels.RequestModels;

namespace MultiAgentsClient.ChatDataStructures;

public class ChatProject : SerializableObjectBase
{
    public int Id;
    public string chatProjectName = NameConsts.DefaultProjectName;
    public string chatProjectSettings = "";
    public bool isChatRoomListExpanded = true;
    public List<ChatRoom> ChatRoomList = [];

    public ChatProject() { }

    public ChatProject(int id, string chatProjectName, string? chatProjectSettings)
    {
        Id = id;
        this.chatProjectName = chatProjectName;
        this.chatProjectSettings = chatProjectSettings ?? "";
    }

    public ChatRoom CreateChatRoom(int chatRoomId, bool AIRenamed, string chatRoomName)
    {
        var chatRoom = new ChatRoom(chatRoomId, AIRenamed, chatRoomName);
        ChatRoomList.Add(chatRoom);
        return chatRoom;
    }

#if FRONTEND
    public ChatProjectDisp ToDisp(int selectedChatRoomId)
    {
        var chatRoomDisp = new List<ChatRoomDisp>();
        foreach (var chatRoom in ChatRoomList)
        {
            if (selectedChatRoomId < 0)
            {
                //if no room is selected in this ChatProject
                chatRoomDisp.Add(chatRoom.ToDisp(false));
            }
            else
            {
                chatRoomDisp.Add(chatRoom.ToDisp(chatRoom.Id == selectedChatRoomId));
            }
        }
        return new ChatProjectDisp(Id, chatProjectName, chatProjectSettings, isChatRoomListExpanded, chatRoomDisp);
    }
#endif
}

public class ChatRoom : SerializableObjectBase
{
    public int Id;
    public string chatRoomName = NameConsts.DefaultChatRoomName;
    public bool AIRenamed = false;
    public bool allowToolCall = false;
    
    [JsonIgnore] public ChatAgent defaultAgent = new();
    [JsonIgnore] public List<ChatAgent> agents = [];

    public ChatRoom() { }

    public ChatRoom(int chatRoomIdIn, bool AIRenamedIn, string chatRoomNameIn)
    {
        Id = chatRoomIdIn;
        AIRenamed = AIRenamedIn;
        chatRoomName = chatRoomNameIn;
    }

#if BACKEND
    public int AddUserMessage(int id, int senderId, string chatMsg, List<int>? historyMessagesIdList, int tokens, string author, DateTime TimeAdded)
    {
        var msgId = id;
        var chatMsgData = ChatMessageData.FromUser(msgId, senderId, chatMsg, historyMessagesIdList,[], [], tokens, author, TimeAdded);
        defaultAgent.ChatListContainer.ChatList.Insert(0, chatMsgData);
        return msgId;
    }
#endif
    
    public void AddChatMessageData(ChatMessageData chatMsgs)
    {
        defaultAgent.ChatListContainer.ChatList.Insert(0, chatMsgs);
    }

#if FRONTEND
    public ChatMessageData AddUserMessage(int messageId, int userId, string message, List<int>? historyMessagesIdList, int tokens, string userName, DateTime dateTime)
    {
        return defaultAgent.AddUserMessage(messageId, userId, message, historyMessagesIdList, tokens, userName, dateTime);
    }

    public ChatMessageData AddSystemMessage(int id, int senderId, string systemMessage, List<int>? historyMessagesIdList, int tokens, DateTime dateTime)
    {
        return defaultAgent.AddSystemMessage(id, senderId, systemMessage, historyMessagesIdList, tokens, dateTime);
    }

    public ChatMessageData AddAssistantMessage(int Id, int senderId, string chatMsg, List<int>? historyMessagesIdList, int tokens, string author, bool streamFinished, DateTime TimeAdded, List<ToolCall> toolCalls)
    {
        return defaultAgent.AddAssistantMsg(Id, senderId, chatMsg, tokens, author, streamFinished, TimeAdded, historyMessagesIdList, toolCalls);
    }

    public ChatMessageData AddToolResultMessage(int Id, int senderId, string toolCallId, string toolCallResult, List<int>? historyMessagesIdList, int tokens, DateTime TimeAdded)
    {
        return defaultAgent.AddToolMsg(Id, senderId, toolCallId, toolCallResult, historyMessagesIdList, tokens, TimeAdded);
    }

    public ChatRoomDisp ToDisp(bool isSelected)
    {
        List<ChatMessageDisp> msgsDisp = [];
        foreach (var msg in defaultAgent.ChatListContainer.ChatList)
        {
            msgsDisp.Add(msg.ToDisp());
        }

        return new ChatRoomDisp(Id, chatRoomName, isSelected, msgsDisp);
    }
#endif
}
